package com.hbnu.test.www.net;

import java.io.*;
import java.net.Socket;

public class TCP_server_thread06 implements Runnable{
private Socket socket;

    public TCP_server_thread06(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        String ip = socket.getInetAddress().getHostAddress();//获取客户端ip
        int count = 1;
        File file = new File("");
        if(file.exists()){
            file = new File(""+ip+"("+(count++)+").jpg");
        }


        try(FileOutputStream fileOutputStream = new FileOutputStream(file);) {
            InputStream in = socket.getInputStream();

            PrintWriter out = new PrintWriter(socket.getOutputStream(), true);

            int len =0;
            byte[] bytes = new byte[1024];
            while((len = in.read(bytes))!= -1){
                fileOutputStream.write(bytes,0,len);
            }
            System.out.println("图片上传成功！");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
